<?php
class Shipping_ShipTypeController extends Zend_Controller_Action
{


	public function indexAction(){
        if($this->_request->isPost()){
            $page = $this->_request->getParam('page',1);
    	    $pageSize = $this->_request->getParam('pageSize',20);
            $st_code = $this->_request->getParam('st_code','');
            $st_class_code = $this->_request->getParam('st_class_code','');
            $st_name_cn = $this->_request->getParam('st_name_cn','');
            $condition = array(
                'st_code'       =>$st_code,
                'st_class_code' =>$st_class_code,
                'st_name_cn'    =>$st_name_cn,
            );
			$page=($page<1)?1:$page;
            $shipTypeList= Shipping_Service_ShipType::getByCondition($condition,'*','st_code',$page,$pageSize);
            $shipTypeList =Shipping_Service_ShipType::getInfo($shipTypeList);
            $count = Shipping_Service_ShipType::getByCondition($condition,'count(*)');
            $json =array('list'=>$shipTypeList,'count'=>$count);
            echo json_encode($json);
            exit;

        }else{

            $stClassCodeList = Shipping_Service_ShipType::getStClassCode();
            $warehouseList = Warehouse_Service_Warehouse::getByCondition();
            $statusList = Shipping_Service_ShipType::getStatusList();

            $this->view->stClassCodeList = $stClassCodeList;
            $this->view->warehouseList = $warehouseList;
            $this->view->statusList = $statusList;
            echo Cff::renderTpl("shipping/shiptype/index.tpl","layout");
        }
    }

    public function infoAction(){
        $st_id = $this->_request->getParam('st_id',0);
        try{
            $shipType = Shipping_Service_ShipType::getByValue($st_id);
            $return = array(
                'ask' =>true,
                'list' =>$shipType,
            );
        }catch(Exception $e){
            $return = array(
                'ask' =>false,
                'list' =>null,
            );
        }
        echo json_encode($return);
        exit;
    }


    public function editAction(){
        $lang = Zend_Registry::get('lang');
        $return = array();
        $st_id = $this->_request->getParam('st_id',0);
        $st_code = $this->_request->getParam('st_code','');
        $st_name_cn = $this->_request->getParam('st_name_cn','');
        $st_name = $this->_request->getParam('st_name','');

        $st_deal_fee = $this->_request->getParam('st_deal_fee',0);
        $st_reg_fee = $this->_request->getParam('st_reg_fee',0);
        $st_addons = $this->_request->getParam('st_addons',0);
        $st_discount = $this->_request->getParam('st_discount',0);

        $st_delivery_time_min = $this->_request->getParam('st_delivery_time_min',0);
        $st_delivery_time_max = $this->_request->getParam('st_delivery_time_max',0);
        $st_vol_weight_rate = $this->_request->getParam('st_vol_weight_rate',0);
        $st_is_volume = $this->_request->getParam('st_is_volume','');

        $st_status = $this->_request->getParam('st_status','');
        $st_class_code = $this->_request->getParam('st_class_code','');
        $st_logo_image = $this->_request->getParam('st_logo_image','');
        $warehouse_id = $this->_request->getParam('warehouse_id','');

        $st_default_reply_address = $this->_request->getParam('st_default_reply_address','');
        $st_discount_min = $this->_request->getParam('st_discount_min',0);
        $st_deal_fee_min = $this->_request->getParam('st_deal_fee_min',0);
        $st_reg_fee_min = $this->_request->getParam('st_reg_fee_min',0);
        $st_sort = $this->_request->getParam('st_sort',0);
        $st_limit_volume = $this->_request->getParam('st_limit_volume',0);

        $val =array(
            'st_id' =>$st_id,
            'st_code' =>$st_code,
            'st_name_cn' =>$st_name_cn,
            'st_name' =>$st_name,

            'st_deal_fee' =>$st_deal_fee,
            'st_reg_fee' =>$st_reg_fee,
            'st_addons' =>$st_addons,
            'st_discount' =>$st_discount,

            'st_delivery_time_min' =>$st_delivery_time_min,
            'st_delivery_time_max' =>$st_delivery_time_max,
            'st_vol_weight_rate' =>$st_vol_weight_rate,
            'st_is_volume' =>$st_is_volume,

            'st_status' =>$st_status,
            'st_class_code' =>$st_class_code,
            'st_logo_image' =>$st_logo_image,
            'warehouse_id' =>$warehouse_id,

            'st_default_reply_address' =>$st_default_reply_address,
            'st_discount_min' =>$st_discount_min,
            'st_deal_fee_min' =>$st_deal_fee_min,
            'st_reg_fee_min' =>$st_reg_fee_min,

            'st_sort' =>$st_sort,
            'st_limit_volume' =>$st_limit_volume,
        );

        $error = Shipping_Service_ShipType::validator($val);
        if(!empty($error)){
            $return['ask'] =false;
            $return['list']['error'] =$error;
            echo Zend_Json::encode($return);
            exit ;
        }
        unset($val['st_id']);
        if($st_id>0){

            if(Shipping_Service_ShipType::update($val,$st_id)){
                $return['ask'] =true;
                $return['list']['message'] =$lang['shipping_method']."{$st_code}".$lang['operation'].$lang['success'];
            }else{
                $return['ask'] =false;
                $return['list']['error'][] =$lang['shipping_method']."{$st_code}".$lang['operation'].$lang['fail'];
            }
        }else{
            if(Shipping_Service_ShipType::add($val)){
                $return['ask'] =true;
                $return['list']['message'][] =$lang['shipping_method']."{$st_code}".$lang['operation'].$lang['success'];
            }else{
                $return['ask'] =false;
                $return['list']['error'][] =$lang['shipping_method']."{$st_code}".$lang['operation'].$lang['fail'];
            }
        }
        echo Zend_Json::encode($return);
        exit ;
    }
    public function deleteAction(){
        $lang = Zend_Registry::get('lang');
        $return = array();
        $st_id = $this->_request->getParam('st_id',0);
        $st_code = $this->_request->getParam('st_code',"");
        if(Shipping_Service_ShipType::delete($st_id)){
            $return['ask'] =true;
            $return['list']['message'] = $lang['shipping_method']."{$st_code}".$lang['operation'].$lang['success'];
        }else{
            $return['ask'] =false;
            $return['list']['error'][] = $lang['shipping_method']."{$st_code}".$lang['operation'].$lang['fail'];
        }
        echo Zend_Json::encode($return);
        exit;
    }
}